
const WeatherApi = require('../api/weatherApi.js')
const { weatherApi, HWeatherApi } = require('../api/index')
var LOCATION = "ip"; // 除拼音外，还可以使用 v3 id、汉语等形式
var argv = require('optimist').default('location', LOCATION).argv;
module.exports = {
    async getWeatherNow (ctx) {
        argv = { ...argv, ...ctx.query }
        if (ctx.query.location) argv.location = ctx.query.location
        await weatherApi.getWeatherNow(argv).then(function (data) {
            // console.log(JSON.stringify(data, null, 4), argv, 'success');
            ctx.body = {
                success: true,
                data: data.results[0],
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            // console.log(err.error, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
        return ctx.body
    },
    async getWeatherHours (ctx) {
        argv = { ...argv, ...ctx.query }
        let weatherApiVaild = new WeatherApi('PFmLX7uvxNfEv-nUt', 'Sbti-v1_CQJK5wBJu'); // 试用13天
        if (ctx.query.location) argv.location = ctx.query.location
        await weatherApiVaild.getWeatherHours(argv).then(function (data) {
            // console.log(JSON.stringify(data, null, 4), argv, 'success');
            ctx.body = {
                success: true,
                data: data.results[0],
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            // console.log(err.error, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
    },
    async getWeatherDaily (ctx) {
        argv = { ...argv, ...ctx.query }
        if (ctx.query.location) argv.location = ctx.query.location
        await weatherApi.getWeatherDaily(argv).then(function (data) {
            // console.log(JSON.stringify(data, null, 4), argv, 'success');
            ctx.body = {
                success: true,
                data: data.results[0],
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            // console.log(err.error, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
    },
    async getWeatherAlarm (ctx) {
        argv = { ...argv, ...ctx.query }
        let weatherApiVaild = new WeatherApi('PFmLX7uvxNfEv-nUt', 'Sbti-v1_CQJK5wBJu'); // 试用13天
        if (ctx.query.location) argv.location = ctx.query.location
        await weatherApiVaild.getWeatherAlarm(argv).then(function (data) {
            // console.log(JSON.stringify(data, null, 4), argv, 'success');
            ctx.body = {
                success: true,
                data: data.results[0],
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            // console.log(err.error, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
    },
    async getWeatherLifeSuggestion (ctx) {
        argv = { ...argv, ...ctx.query }
        if (ctx.query.location) argv.location = ctx.query.location
        await weatherApi.getWeatherLifeSuggestion(argv).then(function (data) {
            // console.log(JSON.stringify(data, null, 4), argv, 'success');
            ctx.body = {
                success: true,
                data: data.results[0],
                message: '操作成功',
                code: 200
            }
        }).catch(function (err) {
            console.log(err.error, 'error');
            ctx.body = {
                success: false,
                message: (err.error && err.error.status) || '服务器异常',
                data: null,
                code: 200
            }
        })
    },
    async initDay (ctx) {
        await HWeatherApi.initDay({ location: ctx.query.location })
            .then(function (data) {
                ctx.body = {
                    success: true,
                    data,
                    message: '操作成功',
                    code: 200
                }
            }).catch(function (err) {
                console.log(err.error, 'error');
                ctx.body = {
                    success: false,
                    message: (err.error && err.error.status) || '服务器异常',
                    data: null,
                    code: 200
                }
            })
    },
    async initDayDetail (ctx) {
        await HWeatherApi.initDayDetail({ location: ctx.query.location })
            .then(function (data) {
                ctx.body = {
                    success: true,
                    data,
                    message: '操作成功',
                    code: 200
                }
            }).catch(function (err) {
                console.log(err.error, 'error');
                ctx.body = {
                    success: false,
                    message: (err.error && err.error.status) || '服务器异常',
                    data: null,
                    code: 200
                }
            })
    },
    async searchCity (ctx) {
        await HWeatherApi.searchCity({ location: ctx.query.location })
            .then(function (data) {
                ctx.body = {
                    success: true,
                    data,
                    message: '操作成功',
                    code: 200
                }
            }).catch(function (err) {
                console.log(err, 'error');
                ctx.body = {
                    success: false,
                    message: (err.error && err.error.status) || '服务器异常',
                    data: null,
                    code: 200
                }
            })
    }
}